from multiprocessing import Queue, Process
import os
import time

# 对比着看  如果是列表的话,每个进程中是独立的一份数据,各不相干
# 如果是queue,数据操作的就是同一份
qisq = False


def worker1(q):
    print('worker1-->', os.getpid())
    if qisq:
        q.put(100)
    else:
        q[0] = 100


def worker2(q, i):
    print('worker2-->', os.getpid())
    if qisq:
        q.put(i)
    else:
        q.append(i)


if __name__ == '__main__':

    if qisq:
        q = Queue()
        q.put(1)
    else:
        q = [1, 2]
    p1 = Process(target=worker1, args=(q,))
    p1.start()
    p1.join()
    for i in range(1, 10):
        p2 = Process(target=worker2, args=(q, i))
        p2.start()
        p2.join()

    # print('main:', x)
    if qisq:
        for i in range(q.qsize()):
            print(q.get())
    else:
        print(q)
    print('over.......')
